package com.qingcheng.dao;

import com.qingcheng.pojo.order.Order;
import org.apache.ibatis.annotations.Param;
import org.apache.ibatis.annotations.Select;
import tk.mybatis.mapper.common.Mapper;

import java.util.List;
import java.util.Map;

public interface OrderMapper extends Mapper<Order> {

    /**
     * 下单人数
     *
     * @param date1
     * @return
     */
    @Select("select count(username) OrderNumber " +
            "from tb_order " +
            "where date_format(create_time, '%Y-%m-%d') = #{date1} " +
            "      and (order_status = 0 or order_status = 1 or order_status = 2) " +
            "group by username")
    public List<Map> getOrderNumber(@Param("date1") String date1);

    /**
     * 订单数， 下单件数， 下单金额
     *
     * @param date1
     * @return
     */
    @Select("select " +
            "  count(id) orderCount, " +
            "  sum(total_num) orderUnitNumber, " +
            "  sum(total_money) orderTotalMoney" +
            "from tb_order " +
            "where date_format(create_time, '%Y-%m-%d') = #{date1}" +
            "      and (order_status = 0 or order_status = 1 or order_status = 2)")
    public List<Map> getOrderCount(@Param("date1") String date1);

    /**
     * 有效订单数
     *
     * @param date1
     * @return
     */
    @Select("select count(id) validOrderCount" +
            "from tb_order " +
            "where date_format(create_time, '%Y-%m-%d') = #{date1}" +
            "      and (order_status = 1 or order_status = 2)")
    public List<Map> getValidOrderCount(@Param("date1") String date1);

    /**
     * 退款金额
     *
     * @param date1
     * @return
     */
    @Select("select sum(return_money) returnMoney" +
            "from tb_return_order " +
            "where date_format(apply_time, '%Y-%m-%d') = #{date1}")
    public List<Map> getReturnMoney(@Param("date1") String date1);

    /**
     * 付款人数
     *
     * @param date1
     * @return
     */
    @Select("select count(username) payCount" +
            "from tb_order " +
            "where date_format(create_time, '%Y-%m-%d') = #{date1} " +
            "      and order_status = 1 " +
            "group by username")
    public List<Map> getPayCount(@Param("date1") String date1);

    /**
     * 付款订单数， 付款件数， 付款金额
     *
     * @param date1
     * @return
     */
    @Select("select " +
            "  count(id) payOrderCount, " +
            "  sum(total_num) payOrderUnitNumber, " +
            "  sum(total_money) payTotalMoney " +
            "from tb_order " +
            "where date_format(create_time, '%Y-%m-%d') = #{date1} " +
            "      and order_status = 1")
    public List<Map> getPayOrderCount(@Param("date1") String date1);

    /**
     * 求汇总
     *
     * @param date1
     * @param date2
     * @return
     */
    @Select("select sum(OrderNumber) orderNumber, " +
            "sum(orderCount) orderCount , " +
            "sum(orderUnitNumber) orderUnitNumber , " +
            "sum(orderTotalMoney) orderTotalMoney , " +
            "sum(validOrderCount) validOrderCount , " +
            "sum(returnMoney) returnMoney , " +
            "sum(payCount) payCount , " +
            "sum(payOrderCount) payOrderCount , " +
            "sum(payOrderUnitNumber) payOrderUnitNumber , " +
            "sum(payTotalMoney) payTotalMoney , " +
            "sum(perCustomerTransaction) perCustomerTransaction , " +
            "sum(viewCount) viewCount " +
            "from tb_order_analyze " +
            "where date_format(orderDate, '%Y-%m-%d') >= #{date1} " +
            "      and date_format(orderDate, '%Y-%m-%d') <= #{date2} " +
            "group by orderDate;")
    public List<Map> getTotal(@Param("date1") String date1, @Param("date2") String date2);


}
